package com.adk.backend.service.impl;

import com.adk.backend.common.PageResult;
import com.adk.backend.entity.LxVndList;
import com.adk.backend.mapper.VndListMapper;
import com.adk.backend.service.VndListService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 供应商列表服务实现类
 */
@Slf4j
@Service
public class VndListServiceImpl extends ServiceImpl<VndListMapper, LxVndList> implements VndListService {
    
    @Autowired
    private VndListMapper vndListMapper;
    
    @Override
    public PageResult<LxVndList> getVndList(Map<String, Object> params) {
        Integer pageNum = (Integer) params.getOrDefault("pageNum", 1);
        Integer pageSize = (Integer) params.getOrDefault("pageSize", 20);
        
        QueryWrapper<LxVndList> wrapper = new QueryWrapper<>();
        
        // 只查询未删除的
        wrapper.eq("is_del", 0);
        
        // 按类型筛选
        if (params.get("type") != null) {
            wrapper.eq("type", params.get("type"));
        }
        
        // 按供应商名称模糊查询
        if (params.get("vndName") != null && !params.get("vndName").toString().isEmpty()) {
            wrapper.like("vnd_name", params.get("vndName"));
        }
        
        // 按简称查询
        if (params.get("shortName") != null && !params.get("shortName").toString().isEmpty()) {
            wrapper.like("short_name", params.get("shortName"));
        }
        
        wrapper.orderByDesc("id");
        
        Page<LxVndList> page = new Page<>(pageNum, pageSize);
        Page<LxVndList> result = vndListMapper.selectPage(page, wrapper);
        
        PageResult<LxVndList> pageResult = new PageResult<>();
        pageResult.setList(result.getRecords());
        pageResult.setTotal(result.getTotal());
        pageResult.setPageNum(pageNum);
        pageResult.setPageSize(pageSize);
        pageResult.setTotalPages((int) result.getPages());
        
        return pageResult;
    }
    
    @Override
    public LxVndList getVndById(Integer id) {
        LxVndList vnd = vndListMapper.selectById(id);
        // 只返回未删除的
        if (vnd != null && vnd.getIsDel() != null && vnd.getIsDel() == 0) {
            return vnd;
        }
        return null;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public LxVndList createVnd(LxVndList vndList) {
        if (vndList.getIsDel() == null) {
            vndList.setIsDel(0);
        }
        if (vndList.getAddTime() == null) {
            vndList.setAddTime((int) (System.currentTimeMillis() / 1000));
        }
        vndListMapper.insert(vndList);
        return vndList;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateVnd(LxVndList vndList) {
        if (vndList.getLastUpdateTime() == null) {
            vndList.setLastUpdateTime((int) (System.currentTimeMillis() / 1000));
        }
        return vndListMapper.updateById(vndList) > 0;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteVnd(Integer id, Integer delUid) {
        LxVndList vnd = vndListMapper.selectById(id);
        if (vnd == null) {
            return false;
        }
        
        // 逻辑删除
        vnd.setIsDel(1);
        vnd.setDelUid(delUid);
        return vndListMapper.updateById(vnd) > 0;
    }
    
    @Override
    public List<LxVndList> getAllVndList(Integer type) {
        QueryWrapper<LxVndList> wrapper = new QueryWrapper<>();
        wrapper.eq("is_del", 0);
        
        if (type != null) {
            wrapper.eq("type", type);
        }
        
        wrapper.orderByDesc("id");
        
        return vndListMapper.selectList(wrapper);
    }
    
    @Override
    public List<LxVndList> getVndListByType(Integer type) {
        QueryWrapper<LxVndList> wrapper = new QueryWrapper<>();
        wrapper.eq("is_del", 0);
        wrapper.eq("type", type);
        // PHP中头程公司按id升序排序（order by id asc）
        wrapper.orderByAsc("id");
        return vndListMapper.selectList(wrapper);
    }
}

